//
//  FoundView.m
//  QuickReadComic
//
//  Created by 苗钧豪 on 16/9/1.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "FoundView.h"
#import "FoundTableViewCell.h"
#import "ZWYNetTool.h"
#import "AFNetworking.h"
#import "FoundTopicsModel.h"
#import "FoundTagsModel.h"
#import "FoundUserModel.h"
#import "UIImageView+WebCache.h"
@implementation FoundView
// 初始化方法
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.array = [NSArray arrayWithObjects:@"0",@"20",@"24",@"22",@"32",@"36",@"23",@"44",@"19",@"27",@"45",@"41", nil];
        NSLog(@"++++++++++++++++++++++++++++++++++++++++%f",self.frame.origin.x);
        [self NetworkRequests];
        // tableview 初始化
        self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) style:(UITableViewStylePlain)];
        // 设置代理人
        _tableview.delegate = self;
        _tableview.dataSource = self;
        // 注册cell
        [_tableview registerNib:[UINib nibWithNibName:@"FoundTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        // 添加到视图上
        [self addSubview:_tableview];
    }
    return self;
}

// 网络请求
- (void)NetworkRequests {
    for (int i = 0; i < 12; i ++) {
        if (self.frame.origin.x / 375 == i) {
            [ZWYNetTool GET:[NSString stringWithFormat:@"http://api.kuaikanmanhua.com/v1/topic_new/lists/get_by_tag?count=20&since=0&tag=%@",_array[i]] andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
                NSLog(@"=====================又成功啦");
                self.model = [FoundDataModel modelWithDic:result[@"data"]];
                [self.tableview reloadData];
            } andFailureBlock:^(NSError *error) {
                NSLog(@"=====================又失败啦");
                NSLog(@"\n**************%@***************\n",error);
            }];
        }
    }
}
// cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 88;
}
// 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
// 每个区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return 20;
    return self.model.topics.count;
}
// 设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    _cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    _topics = self.model.topics[indexPath.row];
    _cell.titleLabel.text = _topics.title;
    _cell.nameLabel.text = _topics.user.nickname;
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [_cell.titleImageView addSubview:btn];
    btn.frame = CGRectMake(0, 0, _cell.titleImageView.bounds.size.width, _cell.titleImageView.bounds.size.height);
    [btn setTitle:@"" forState:(UIControlStateNormal)];
    if ([[NSUserDefaults standardUserDefaults] integerForKey:@"isOn"] == 0) {
        btn.userInteractionEnabled = NO;
        [btn setBackgroundColor:[UIColor clearColor]];
    } else {
        btn.userInteractionEnabled = YES;
        [btn setBackgroundColor:[UIColor grayColor]];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventValueChanged)];
    }
    [_cell.titleImageView sd_setImageWithURL:[NSURL URLWithString:_topics.cover_image_url]];
    _cell.goodLabel.text = [NSString stringWithFormat:@"%ld",(long)_topics.likes_count];
    _cell.sayLabel.text = [NSString stringWithFormat:@"%ld",(long)_topics.comments_count];
//    [self createImageTableView:tableView cellForRowAtIndexPath:indexPath];
    return _cell;
}
- (void)btnAction:(UIButton *)button {
    [button setBackgroundColor:[UIColor clearColor]];
    button.userInteractionEnabled = NO;
}
- (void)tapImage:(UIImageView *)imageView {
    UIView *v = [imageView superview];//UITableViewCellContentView
    FoundTableViewCell* cell = (FoundTableViewCell *)[v superview];//UITableViewCell
//    NSIndexPath *indexPath= [_tbSelect indexPathForCell:cell];
    [cell.titleImageView sd_setImageWithURL:[NSURL URLWithString:_topics.cover_image_url]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
